package com.ecar.common.datetime;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 全局格式化数据类
 * @author tianpeng
 */
public class FormatData {
	
	/**
	 * 将时间改成字符串
	 * @param date
	 * @return
	 */
	public String dateToStr1(Date date){
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
	    String dateStr = formatter.format(date);
	    return dateStr;
	}
	
	/**
	 * 将时间改成字符串
	 * @param date
	 * @return
	 */
	public String dateToStr2(Date date) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
		String dateStr = formatter.format(date);
		return dateStr;
	}
	
	public String dateToStr3(Date date){
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String dateStr = formatter.format(date);
	    return dateStr;
	}
	
	/**
	 * 将字符串转换成日期
	 * @param str
	 * @return
	 */
	public Date strToDate(String str) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = formatter.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 将字符串转换成List<Long>
	 * @param str: 以逗号分割的字符串
	 * @return
	 */
	public List<Long> strToList(String str) {
		String[] strArray = str.split(",");
		Long[] longArray = stringArrayToLongArray(strArray);
		List<Long> list = new ArrayList<>();
		for (int i=0; i<longArray.length; i++) {
			list.add(longArray[i]);
		}
		return list;
	}
	
	/**
	 * 将字符串转换成长整型数组
	 * @return
	 */
	public Long[] strToLongArray(String str) {
		String[] strArray = str.split(",");
		return stringArrayToLongArray(strArray);
	}
	
	/**
	 * 将字符串数组转换成长整型数组
	 * @param strArray
	 * @return
	 */
	public Long[] stringArrayToLongArray(String[] strArray) {
		if (strArray.length == 0) {
			return null;
		}
		Long[] longArray = new Long[strArray.length];
		for (int i = 0; i < strArray.length; i++) {
			try {
				Long temp = Long.parseLong(strArray[i]);
				longArray[i] = temp;
			} catch (Exception e) {
			}
		}
		return longArray;
	}
	
	public String DateString14ToDateString20(String dateString14){
		String dateString20 = "";
		String year = dateString14.substring(0, 4);
		String month = dateString14.substring(4, 6);
		String day = dateString14.substring(6, 8);
		String hh = dateString14.substring(8, 10);
		String mm = dateString14.substring(10, 12);
		String ss = dateString14.substring(12, 14);
		dateString20 = year+"-"+month+"-"+day+" "+hh+":"+mm+":"+ss;
	    return dateString20;
	}
	
	public String getFileName(String fileName) {
		int flag = 0, i = 0;
		String fn = "";
		for (i = 0; i < fileName.length(); ++i) {
			if (fileName.substring(i, i + 1).matches("[\\u4e00-\\u9fa5]+")) {
				flag += 2;
			} else {
				++flag;
			}
			fn += fileName.substring(i, i + 1);
			if (flag == 8)
				break;
		}
		if(fileName.length()>fn.length()) fn = fn+"...";
		return fn;
	}
}